import tensorflow as tf
import numpy as np

# 构建DQN模型
model = tf.keras.Sequential([
    tf.keras.layers.Dense(64, activation='relu', input_shape=(2,)),
    tf.keras.layers.Dense(2, activation='linear')
])

model.compile(optimizer='adam', loss='mse')

# 环境和DQN算法
class Environment:
    def __init__(self):
        self.state = 0

    def step(self, action):
        reward = 1 if action == 0 else -1
        self.state = 1 - self.state
        return self.state, reward

env = Environment()
epsilon = 0.1
gamma = 0.9
alpha = 0.1

for episode in range(100):
    state = np.array([0])
    done = False
    while not done:
        if np.random.rand() < epsilon:
            action = np.random.choice([0, 1])
        else:
            action = np.argmax(model.predict(state.reshape(1, -1)))

        next_state, reward = env.step(action)
        next_state = np.array([next_state])

        target = reward + gamma * np.max(model.predict(next_state.reshape(1, -1)))
        target_f = model.predict(state.reshape(1, -1))
        target_f[0][action] = target

        model.fit(state.reshape(1, -1), target_f, epochs=1, verbose=0)

        state = next_state

print("Training complete!")